<?php
@header('Content-Type: text/html; charset=utf-8');

include_once("chart_include.php");
printr($_POST);
if(0 != count($_POST))
{
	$dimension = $_POST['dimension'];
	$chartType = $_POST['chartType'];
	$isRegister = $_POST['register']; 
	$sex = $_POST['sex']; 
	$agetype = $_POST['agetype'];

	/*时间过滤*/
	$statinfo['start_time'] = str_pad($_POST['statis_from_year'], 4, '0', STR_PAD_LEFT)."-".str_pad($_POST['statis_from_month'], 2, '0', STR_PAD_LEFT)."-".str_pad($_POST['statis_from_day'], 2, '0', STR_PAD_LEFT)." 00:00:00";
	$statinfo['end_time'] = str_pad($_POST['statis_to_year'], 4, '0', STR_PAD_LEFT)."-".str_pad($_POST['statis_to_month'], 2, '0', STR_PAD_LEFT)."-".str_pad($_POST['statis_to_day'], 2, '0', STR_PAD_LEFT)." 23:59:59";

	/*用户基本信息过滤*/
	$userinfo = array();
	$userinfo['sex'] = (null==$_POST['sex'])?null:$sex;
	if(0 == strcmp("all",$agetype)){
		$userinfo['fromage'] = null;
		$userinfo['toage'] = null;
	}else {
		$userinfo['fromage'] = $_POST['fromage'];
		$userinfo['toage'] = $_POST['toage'];
	}
	$userinfo['start_time'] = str_pad($_POST['join_from_year'], 4, '0', STR_PAD_LEFT)."-".str_pad($_POST['join_from_month'], 2, '0', STR_PAD_LEFT)."-".str_pad($_POST['join_from_day'], 2, '0', STR_PAD_LEFT)." 00:00:00";
	$userinfo['end_time'] = str_pad($_POST['join_to_year'], 4, '0', STR_PAD_LEFT)."-".str_pad($_POST['join_to_month'], 2, '0', STR_PAD_LEFT)."-".str_pad($_POST['join_to_day'], 2, '0', STR_PAD_LEFT)." 23:59:59";

	if(0 == strcmp('keyword', $dimension))
	{
		$result = GetKeywordMaxs(7, $statinfo, $userinfo, $isRegister);
		printr($result);

		if(0 == strcmp($chartType,'pie')){
			$scoreList = array(); $labelList = array();
			foreach($result as $key => $value){
				array_push($scoreList, $value);
				array_push($labelList, $key.": ".$value);
			}

			WxDrawPie3D($scoreList, $labelList, "关键字搜索统计");
		}else if(0 == strcmp($chartType,'line')){
			$scoreList = array(); $labelList = array();
			foreach($result as $key => $value){
				array_push($scoreList, $value);
				array_push($labelList, $key);
			}

			WxDrawLine($scoreList,"关键字",$labelList,"搜索频度","关键字分析");
		}else if(0 == strcmp($chartType,'bar')){
			$scoreList = array(); $labelList = array();
			foreach($result as $key => $value){
				array_push($scoreList, $value);
				array_push($labelList, $key);
			}

			WxDrawBar($scoreList,"关键字",$labelList,"搜索频度","关键字分析");
		}
	}else if(0 == strcmp('product', $dimension)){
		$result = GetProductMaxs(7, $statinfo, $userinfo);
		$scoreList = array(); $labelList = array();
		foreach($result as $key => $value){
			array_push($scoreList, $value);
			array_push($labelList, $key);
		}
		
		if(0 == strcmp($chartType,'pie')){
			WxDrawPie3D($scoreList, $labelList, "销量统计");
		}else if(0 == strcmp($chartType,'line')){
			WxDrawLine($scoreList, "产品", $labelList, "销量","销量统计");
		}else if(0 == strcmp($chartType,'bar')){	
			WxDrawBar($scoreList, "产品", $labelList, "销量","销量统计");
		}
	}else if(0 == strcmp('time', $dimension)){

		$duration['interval_type'] = $_POST['interval_type'];

		$duration['start_time'] = str_pad($_POST['from_year'], 4, '0', STR_PAD_LEFT)."-".str_pad($_POST['from_month'], 2, '0', STR_PAD_LEFT)."-".str_pad($_POST['from_day'], 2, '0', STR_PAD_LEFT)." 00:00:00";
		$duration['end_time']  = str_pad($_POST['to_year'], 4, '0', STR_PAD_LEFT)."-".str_pad($_POST['to_month'], 2, '0', STR_PAD_LEFT)."-".str_pad($_POST['to_day'], 2, '0', STR_PAD_LEFT)." 23:59:59";		

		$result = GetKeywordByTime($duration);
		$scoreList = array(); $labelList = array();
		foreach($result as $key => $value){
			array_push($scoreList, $value);
			array_push($labelList, $key);
		}

		$temp = $$_POST['datavalue'];
		if(0 == strcmp($chartType,'pie')){
			WxDrawPie3D($scoreList, $labelList, "时间统计");
		}else if(0 == strcmp($chartType,'line')){
			WxDrawLine($scoreList, $temp, $labelList, "数量","时间统计");
		}else if(0 == strcmp($chartType,'bar')){	
			WxDrawBar($scoreList, $temp, $labelList, "数量","时间统计");
		}
	}
	display_full(null);
	echo "<div align=\"center\"><input type=\"image\"
	   src=\"pictures/temp.png\"
	   alt=\"".$alt."\" border=\"0\" /></div>";
}else
{
	display_full(null);
}

function display_form_button($image, $alt) {
  echo "<div align=\"center\"><input type=\"image\"
           src=\"".$image.".gif\"
           alt=\"".$alt."\" border=\"0\" height=\"50\"
           width=\"135\"/></div>";
}
?>

<p id="demo">My First Paragraph</p>

<script>
function myFunction()
{
  var myselect = document.getElementById("dimension");
  document.getElementById("demo").innerHTML=myselect.value;
  alert(myselect);  
}
</script>


<?php
function display_full() {
  display_keyword_seg();
  /*display_product_seg();
  display_time_seg();*/
}
?>



<?php
function display_keyword_seg() {
?>
<table border="0" width="100%" cellspacing="0">
  <form action="#" method="post">
  <tr><th colspan="2" bgcolor="#cccccc">关键字 数据分析</th></tr>
  <tr>
	<td>
	<input ID="pie" type="radio" name="chartType" value="pie" checked /><label>饼图</label>
	<input ID="line" type="radio" name="chartType" value="line" /><label>折线图</label>
	<input ID="bar" type="radio" name="chartType" value="bar" /><label>柱状图</label>
	</td>
  </tr>
  <tr>
    <td>分析对象</td>
    <td><select id="dimension" name="dimension" onChange="myFunction()" >
        <option value="keyword" >关键字</option>
		<option value="product" >产品</option>
        </select>
    </td>
  </tr>

  <tr>
    <td>时间跨度（筛选条件）</td>
    <td>
 
	<? display_date_interval("statis_from_year","statis_from_month", "statis_from_day", "statis_to_year","statis_to_month", "statis_to_day"); ?>

	</td>
  </tr>
  
  <tr>
	<td>收录关键字（筛选条件）</td>
	<td>
	<input type="radio" name="register" value="0" /><label>所有</label>
	<input type="radio" name="register" value="1" checked /><label>是</label>
	<input type="radio" name="register" value="2" /><label>否</label>
	</td>
  </tr>
  
  <tr>
	<td>性别（筛选条件）</td>
	<td>
	<input type="radio" name="sex" value=0 checked /><label>所有</label>
	<input type="radio" name="sex" value=1 /><label>男</label>
	<input type="radio" name="sex" value=2 /><label>女</label>
	</td>
  </tr>
  <tr>
    <td>年龄（筛选条件）</td>
	<td>
	<input type="radio" name="agetype" value="all" checked /><label>所有</label>
	<input type="radio" name="agetype" value="ageSelfDefined" /><label>自定义：</label>
	<input type="text" name="fromage" size="4" />-<input type="text" name="toage" size="4"/>
	</td>
  </tr>
  <tr>
    <td>加入时间（筛选条件）</td>
	<td>
	<? display_date_interval("join_from_year","join_from_month", "join_from_day", "join_to_year","join_to_month", "join_to_day"); ?>
	</td>
  </tr>
  
  
  <tr>
    <td colspan="2" align="center">
      <p><strong>Please press Continue to data analysis!</strong></p>
     <?php display_form_button('continue', 'Generate chart'); ?>
    </td>
  </tr>
  </form>
  </table>
<?php
}
?>



<?php
function display_product_seg() {
?>
<table border="0" width="100%" cellspacing="0">
  <form action="pChart/examples/chart_process.php" method="post">
  <tr><th colspan="2" bgcolor="#cccccc">产品数据分析</th></tr>
  <tr>
	<td>
	<input ID="pie" type="radio" name="chartType" value="pie" checked /><label>饼图</label>
	<input ID="line" type="radio" name="chartType" value="line" /><label>折线图</label>
	<input ID="bar" type="radio" name="chartType" value="bar" /><label>柱状图</label>
	</td>
  </tr>
  <tr>
    <td>分析对象</td>
    <td><select name="dimension">
        <option value="product" >产品</option>
        </select>
    </td>
  </tr>

  <tr>
    <td>时间跨度（筛选条件）</td>
    <td>
 
	<? display_date_interval("statis_from_year","statis_from_month", "statis_from_day", "statis_to_year","statis_to_month", "statis_to_day"); ?>

	</td>
  </tr>
  
  <tr>
	<td>收录关键字（筛选条件）</td>
	<td>
	<input type="radio" name="register" value="0" /><label>所有</label>
	<input type="radio" name="register" value="1" checked /><label>是</label>
	<input type="radio" name="register" value="2" /><label>否</label>
	</td>
  </tr>
  
  <tr>
	<td>性别（筛选条件）</td>
	<td>
	<input type="radio" name="sex" value=0 checked /><label>所有</label>
	<input type="radio" name="sex" value=1 /><label>男</label>
	<input type="radio" name="sex" value=2 /><label>女</label>
	</td>
  </tr>
  <tr>
    <td>年龄（筛选条件）</td>
	<td>
	<input type="radio" name="agetype" value="all" checked /><label>所有</label>
	<input type="radio" name="agetype" value="ageSelfDefined" /><label>自定义：</label>
	<input type="text" name="fromage" size="4" />-<input type="text" name="toage" size="4"/>
	</td>
  </tr>
  <tr>
    <td>加入时间（筛选条件）</td>
	<td>
	<? display_date_interval("join_from_year","join_from_month", "join_from_day", "join_to_year","join_to_month", "join_to_day"); ?>
	</td>
  </tr>
  
  
  <tr>
    <td colspan="2" align="center">
      <p><strong>Please press Continue to data analysis!</strong></p>
     <?php display_form_button('continue', 'Generate chart'); ?>
    </td>
  </tr>
  </form>
  </table>
<?php
}
?>



<?php
function display_time_seg() {
?>
<table border="0" width="100%" cellspacing="0">
  <form action="pChart/examples/chart_process.php" method="post">
  <tr><th colspan="2" bgcolor="#cccccc">时间 数据分析</th></tr>
  <tr>
	<td>
	<input ID="pie" type="radio" name="chartType" value="pie" checked /><label>饼图</label>
	<input ID="line" type="radio" name="chartType" value="line" /><label>折线图</label>
	<input ID="bar" type="radio" name="chartType" value="bar" /><label>柱状图</label>
	</td>
  </tr>
  <tr>
    <td>分析对象</td>
    <td><select name="dimension">
        <option value="time" >时间</option>
        </select>
    </td>
  </tr>

  <tr>
    <td>数据</td>
    <td><select name="datavalue">
        <option value="keyword" >关键字</option>
		<option value="product" >产品</option>
        </select>
    </td>
  </tr>
  
  <tr>
    <td>时间间隔</td>
	<td>
	<input type="radio" name="interval_type" value="hour" /><label>按时</label>
	<input type="radio" name="interval_type" value="day" /><label>按日</label>
	<input type="radio" name="interval_type" value="weekday" checked /><label>按星期</label>
	<input type="radio" name="interval_type" value="month" /><label>按月</label>
	</td>
  </tr>
  <tr>
  <td>时间跨度</td>
    <td>
	<? display_date_interval("from_year","from_month", "from_day", "to_year","to_month", "to_day"); ?>
	</td>
  </tr>
  
  <tr>
    <td colspan="2" align="center">
      <p><strong>Please press Continue to data analysis!</strong></p>
     <?php display_form_button('continue', 'Generate chart'); ?>
    </td>
  </tr>
  </form>
  </table>
<?php
}
?>



<?php
function display_date_interval($from_year, $from_month, $from_day, $to_year, $to_month, $to_day) {
?>
From
<select name=<? echo "'".$from_year."'";?> >
<?
for ($y = 2013; $y < 2015; $y++) {
	echo "<option value=\"".$y."\">".$y."</option>";
}
?>
</select>
<select name=<? echo "'".$from_month."'";?> >
<?
$current_m = date('m');
for ($m = 1; $m <= 12; $m++) {
	if($m == intval($current_m, 9)){
		echo "<option value=\"".$m."\" selected=\"selected\" >".$m."</option>";
	}else{
		echo "<option value=\"".$m."\">".$m."</option>";
	}
}
?>
</select>
<select name=<? echo "'".$from_day."'";?> >
<?
for ($d = 1; $d <= 31; $d++) {
	if($d == 1){
		echo "<option value=\"".$d."\" selected=\"selected\" >".$d."</option>";
	}else{
		echo "<option value=\"".$d."\">".$d."</option>";
	}
}
?>
</select>
To
<select name=<? echo "'".$to_year."'";?> >
<?
for ($y = 2013; $y < 2015; $y++) {
	echo "<option value=\"".$y."\">".$y."</option>";
}
?>
</select>
<select name=<? echo "'".$to_month."'";?> >
<?
$current_m = date('m');
for ($m = 1; $m <= 12; $m++) {
	if($m == intval($current_m, 9)){
		echo "<option value=\"".$m."\" selected=\"selected\" >".$m."</option>";
	}else{
		echo "<option value=\"".$m."\">".$m."</option>";
	}
}
?>
</select>
<select name=<? echo "'".$to_day."'";?> >
<?
for ($d = 1; $d <= 31; $d++) {
	if($d == 30){
		echo "<option value=\"".$d."\" selected=\"selected\" >".$d."</option>";
	}else{
		echo "<option value=\"".$d."\">".$d."</option>";
	}
}
?>
</select>
<?
}
?>


<?php
/*function printr($data) {
   echo "<pre>";
      $temp_print_r = print_r($data,true);
	  DEBUGINFO($temp_print_r);
   echo "</pre>";
}

function DEBUGINFO($abc)
{
	@ $fp = fopen("debugchart.txt", 'ab');
	fwrite($fp, $abc);
	fclose($fp);
}*/
?>
